from plugin.dk.Base import Base
from . import docker

class NetWork(Base):
    def __init__(self):
        pass

    def getList(self):
        """
        获取一个网络列表
        :return:
        """
        data = docker.networks.list()

        result = []
        for value in data:
            result.append(value.attrs)
        return result if len(result) is not 0 else None

    def connect(self, network, containerId, **kwargs):
        """
        链接到网络
        :param network:
        :param containerId:
        :param kwargs:
         Args:
            container (str): Container to connect to this network, as either
                an ID, name, or :py:class:`~docker.models.containers.Container`
                object.
            aliases (:py:class:`list`): A list of aliases for this endpoint.
                Names in that list can be used within the network to reach the
                container. Defaults to ``None``.
            links (:py:class:`list`): A list of links for this endpoint.
                Containers declared in this list will be linkedto this
                container. Defaults to ``None``.
            ipv4_address (str): The IP address of this container on the
                network, using the IPv4 protocol. Defaults to ``None``.
            ipv6_address (str): The IP address of this container on the
                network, using the IPv6 protocol. Defaults to ``None``.
            link_local_ips (:py:class:`list`): A list of link-local (IPv4/IPv6)
                addresses.
        :return:
        """
        contaiers = docker.networks.get(network)
        contaiers.disconnect(containerId)
        return contaiers.connect(containerId, **kwargs)

    def create(self, name, **kwargs):
        """
        创建一个网络
        :param name:
        :param kwargs:
        :return:
        """
        return docker.networks.create(name, **kwargs)
    def remove(self,networkId):
        network = docker.networks.get(networkId)
        return network.remove()
